package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.ImageTypeEnum;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.ChangeNextStatusUtil;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.model.WechatIntegration;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.InstMchntRegisterRequest;
import cc.rengu.igas.bomp.facade.response.InstMchntRegisterResponse;
import cc.rengu.igas.channel.alipay.facade.bean.AddressInfo;
import cc.rengu.igas.channel.alipay.facade.bean.BankCardInfo;
import cc.rengu.igas.channel.alipay.facade.bean.ContactInfo;
import cc.rengu.igas.channel.alipay.facade.bean.SignInfo;
import cc.rengu.igas.channel.alipay.facade.request.AliCreateMerchantRequest;
import cc.rengu.igas.channel.alipay.facade.request.AliGetAppAuthCodeRequest;
import cc.rengu.igas.channel.alipay.facade.request.AliUpdateMerchantRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliCreateMerchantResponse;
import cc.rengu.igas.channel.alipay.facade.response.AliGetAppAuthCodeResponse;
import cc.rengu.igas.channel.alipay.facade.response.AliUpdateMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.WechatCreateMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.request.WechatUpdateMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatCreateMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.response.WechatUpdateMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: zhangxuran
 * @Date: 2020/4/7 22:12
 * @Description: 第三方机构商户进件 （查询第三方商户信息表，调用第三方进件接口，目前支持微信，支付宝）
 * 本功能现只支持普通商户（商户基础信息表中的商户）
 */
public class InstMchntRegisterService extends ManageService {

    private static String WX_CHANNEL_FLAG = "01";

    private static String ALI_CHANNEL_FLAG = "02";

    private static String PLAT_MCHNT_TYPE = "01";

    @Override
    protected Object bizBeforeCust() throws Exception {
        InstMchntRegisterRequest instMchntRegisterRequest = new InstMchntRegisterRequest();
        ConvertUtil.convertOutput(instMchntRegisterRequest);
        return instMchntRegisterRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        InstMchntRegisterRequest instMchntRegisterRequest = (InstMchntRegisterRequest) request;
        BizResponse<InstMchntRegisterResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        InstMchntRegisterResponse instMchntRegisterResponse = new InstMchntRegisterResponse();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        if (PLAT_MCHNT_TYPE.equals(instMchntRegisterRequest.getMchntBizFlag())) {
            //平台商户
            PlatMchntInfoMapper platMchntInfoMapper = new PlatMchntInfoMapperImpl();
            PlatMchntInfo platMchntInfo = platMchntInfoMapper.selectPlatMchntInfoByMchntNo(instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
            if (null == platMchntInfo) {
                rglog.error("平台商户信息查询失败！instId:<{}>,mchntNo:<{}>",
                        instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
            }

            if (!MchntStatusEnum.NORMAL.getStatus().equals(platMchntInfo.getPlatMchntStatus())) {
                rglog.error("当前平台商户状态为非正式商户！instId:<{}>,mchntNo:<{}>",
                        instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
            }

            if (WX_CHANNEL_FLAG.equals(instMchntRegisterRequest.getChannelFlag())) {
                //微信入驻或修改
                wechatCreateMchntOrUpdateMchnt(instMchntRegisterRequest, sysSeqNum, platMchntInfo);
            } else if (ALI_CHANNEL_FLAG.equals(instMchntRegisterRequest.getChannelFlag())) {
                AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
                AlipayMchntInfo alipayMchntInfo = alipayMchntInfoMapper.selectAlipayMchntInfoByPrimaryKey(
                        instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                if (null == alipayMchntInfo) {
                    rglog.error("支付宝商户信息查询失败！instId:<{}>,mchntNo:<{}>",
                            instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                    throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
                }
                boolean alipayInDirectFlag = txnInfo.getChannelCallCfgList().stream().anyMatch(item ->
                        item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()));
                if (!alipayInDirectFlag && StringUtil.isEmptyOrNull(alipayMchntInfo.getAppAuthToken())) {
                    //直联模式，先返回商户应用授权URL
                    rglog.info("直联支付宝进件需要先获取商户应用授权，返回商户应用授权连接URL");
                    String mchntAppAuthUrl = getAlipayMchntAppAuth(instMchntRegisterRequest, alipayMchntInfo);
                    instMchntRegisterResponse.setMchntAppAuthUrl(mchntAppAuthUrl);
                } else {
                    //间联模式，直接进行支付宝入驻或修改
                    alipayCreateMchntOrUpdateMchntByPlatMchntInfo(instMchntRegisterRequest, sysSeqNum, platMchntInfo, alipayMchntInfo);
                }
            }
        } else {
            //间联商户
            MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
            MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(
                    instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
            if (null == mchntBaseInfo) {
                rglog.error("商户基础信息查询失败！instId:<{}>,mchntNo:<{}>",
                        instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
            }

            if (!MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
                rglog.error("当前商户状态为非正式商户！instId:<{}>,mchntNo:<{}>",
                        instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
            }


            if (CollectionUtils.isEmpty(txnInfo.getChannelCallCfgList())) {
                rglog.error("第三方机构商户进件处理失败，txnInfo.getchannelCallCfgList 通道服务调用配置为空!");
                throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
            }


            if (WX_CHANNEL_FLAG.equals(instMchntRegisterRequest.getChannelFlag())) {
                //微信入驻或修改
                wechatCreateMchntOrUpdateMchnt(instMchntRegisterRequest, sysSeqNum, mchntBaseInfo);
            } else if (ALI_CHANNEL_FLAG.equals(instMchntRegisterRequest.getChannelFlag())) {
                AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
                AlipayMchntInfo alipayMchntInfo = alipayMchntInfoMapper.selectAlipayMchntInfoByPrimaryKey(
                        instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                if (null == alipayMchntInfo) {
                    rglog.error("支付宝商户信息查询失败！instId:<{}>,mchntNo:<{}>",
                            instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
                    throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
                }
                boolean alipayInDirectFlag = txnInfo.getChannelCallCfgList().stream().anyMatch(item ->
                        item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()));
                if (!alipayInDirectFlag && StringUtil.isEmptyOrNull(alipayMchntInfo.getAppAuthToken())) {
                    //直联模式，先返回商户应用授权URL
                    rglog.info("直联支付宝进件需要先获取商户应用授权，返回商户应用授权连接URL");
                    String mchntAppAuthUrl = getAlipayMchntAppAuth(instMchntRegisterRequest, alipayMchntInfo);
                    instMchntRegisterResponse.setMchntAppAuthUrl(mchntAppAuthUrl);
                } else {
                    //支付宝入驻或修改
                    alipayCreateMchntOrUpdateMchnt(instMchntRegisterRequest, sysSeqNum, mchntBaseInfo, alipayMchntInfo);
                }
            }
        }


        bizResponse.setResult(instMchntRegisterResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());


        return bizResponse;
    }


    private void alipayCreateMchntOrUpdateMchntByPlatMchntInfo(InstMchntRegisterRequest instMchntRegisterRequest,
                                                               String sysSeqNum, PlatMchntInfo platMchntInfo, AlipayMchntInfo alipayMchntInfo) throws Exception {
        //判断当前状态是否允许修改
        if (!ChangeNextStatusUtil.isAllowModifyData(alipayMchntInfo.getAuditStatus())) {
            rglog.error("第三方机构商户进件处理失败, 当前支付宝商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                    alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getAuditStatus());
            throw new BizException(RespCodeEnum.MANAGE_OPERATE_ERROR.getRespCode(), RespCodeEnum.MANAGE_OPERATE_ERROR.getRespDesc());
        }

        if ("0".equals(instMchntRegisterRequest.getOprType())) {
            //获取通道服务调用配置- 支付宝
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg aliChannelCallCfg = channelCallCfgList.get(0);
            //调用支付宝进行商户入驻
            AliCreateMerchantRequest aliCreateMerchantRequest = initAliCreateMerchantRequestByPlatMchntInfo(instMchntRegisterRequest, sysSeqNum, platMchntInfo, alipayMchntInfo, aliChannelCallCfg.getCallChannelId(), aliChannelCallCfg);
            DubboService dubboService = new DubboServiceImpl();
            Object object = dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliCreateMerchant", aliCreateMerchantRequest);
            cc.rengu.igas.channel.alipay.facade.result.Result<AliCreateMerchantResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliCreateMerchantResponse>) object;
            if (null == aliResult) {
                rglog.error("支付宝商户入驻, 当前支付宝商户信息入驻失败!");
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else if (!aliResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
                rglog.error("支付宝商户入驻, 当前支付宝商户信息入驻失败，instId:{},mchntNo:{},aliResult:{}.",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
                throw new BizException(aliResult.getResult().getRespCode(), aliResult.getResult().getRespDesc());
            }

            //更新商户支付宝信息表与历史数据表
            alipayMchntInfo.setAlipayMchntNo(aliResult.getResult().getAliMchntNo());
            if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                alipayMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
            }
            BizHistory bizHistory = new BizHistory();
            BeanUtil.beanCopy(alipayMchntInfo, bizHistory);
            bizHistory.setManageInstId(platMchntInfo.getManageInstId());
            bizHistory.setMchntName(platMchntInfo.getPlatMchntName());
            bizHistory.setRealId(alipayMchntInfo.getId());
            bizHistory.setOprTableEname(TableNameConstant.T_B_ALPAY_MCHNT_INFO);
            //修改
            bizHistory.setBizOpr("U");
            bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
            String modifyAliMchntInfoJson = JSONObject.toJSONString(alipayMchntInfo);

            //将正式表的商户信息存入历史表 bizData
            InitParamUtil.initBizHistoryData(bizHistory, modifyAliMchntInfoJson);
            modifyAliMchntInfoData(alipayMchntInfo, bizHistory);
        } else {
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg aliChannelCallCfg = channelCallCfgList.get(1);

            //调用支付宝进行商户修改
            AliCreateMerchantRequest aliCreateMerchantRequest = initAliCreateMerchantRequestByPlatMchntInfo(instMchntRegisterRequest, sysSeqNum, platMchntInfo, alipayMchntInfo, aliChannelCallCfg.getCallChannelId(), aliChannelCallCfg);
            AliUpdateMerchantRequest aliUpdateMerchantRequest = new AliUpdateMerchantRequest();
            //数据一致
            BeanUtil.beanCopy(aliCreateMerchantRequest, aliUpdateMerchantRequest);
            aliUpdateMerchantRequest.setHeader(aliCreateMerchantRequest.getHeader());
            DubboService dubboService = new DubboServiceImpl();
            Object object = dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliUpdateMerchant", aliUpdateMerchantRequest);
            cc.rengu.igas.channel.alipay.facade.result.Result<AliUpdateMerchantResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliUpdateMerchantResponse>) object;
            if (null == aliResult) {
                rglog.error("支付宝商户信息修改, 当前支付宝商户信息入驻失败，instId:{},mchntNo:{},aliResult:{}.",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else if (!aliResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
                rglog.error("支付宝商户信息修改, 当前支付宝商户信息入驻失败，instId:{},mchntNo:{},aliResult:{}.",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
                throw new BizException(aliResult.getResult().getRespCode(), aliResult.getResult().getRespDesc());
            }

            //更新商户支付宝信息表与历史数据表
            if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                alipayMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
            }
            BizHistory bizHistory = new BizHistory();
            BeanUtil.beanCopy(alipayMchntInfo, bizHistory);
            bizHistory.setManageInstId(platMchntInfo.getManageInstId());
            bizHistory.setMchntName(platMchntInfo.getPlatMchntName());
            bizHistory.setRealId(alipayMchntInfo.getId());
            bizHistory.setOprTableEname(TableNameConstant.T_B_ALPAY_MCHNT_INFO);
            bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
            //修改
            bizHistory.setBizOpr("U");
            String modifyAliMchntInfoJson = JSONObject.toJSONString(alipayMchntInfo);

            //将正式表的商户信息存入历史表 bizData
            InitParamUtil.initBizHistoryData(bizHistory, modifyAliMchntInfoJson);
            modifyAliMchntInfoData(alipayMchntInfo, bizHistory);
        }
    }


    private void alipayCreateMchntOrUpdateMchnt(InstMchntRegisterRequest instMchntRegisterRequest, String sysSeqNum, MchntBaseInfo mchntBaseInfo, AlipayMchntInfo alipayMchntInfo) throws Exception {
        //判断当前状态是否允许修改
        if (!ChangeNextStatusUtil.isAllowModifyData(alipayMchntInfo.getAuditStatus())) {
            rglog.error("第三方机构商户进件处理失败, 当前支付宝商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                    alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getAuditStatus());
            throw new BizException(RespCodeEnum.MANAGE_OPERATE_ERROR.getRespCode(), RespCodeEnum.MANAGE_OPERATE_ERROR.getRespDesc());
        }

        if ("0".equals(instMchntRegisterRequest.getOprType())) {
            //获取通道服务调用配置- 支付宝
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg aliChannelCallCfg = channelCallCfgList.get(0);

            //调用支付宝进行商户入驻
            AliCreateMerchantRequest aliCreateMerchantRequest = initAliCreateMerchantRequest(instMchntRegisterRequest, sysSeqNum, mchntBaseInfo, alipayMchntInfo, aliChannelCallCfg.getCallChannelId(), aliChannelCallCfg);
            DubboService dubboService = new DubboServiceImpl();
            Object object = dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliCreateMerchant", aliCreateMerchantRequest);
            cc.rengu.igas.channel.alipay.facade.result.Result<AliCreateMerchantResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliCreateMerchantResponse>) object;
            if (null == aliResult) {
                rglog.error("支付宝商户入驻, 当前支付宝商户信息入驻失败!");
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else if (!aliResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
                rglog.error("支付宝商户入驻, 当前支付宝商户信息入驻失败，instId:{},mchntNo:{},aliResult:{}.",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
                throw new BizException(aliResult.getResult().getRespCode(), aliResult.getResult().getRespDesc());
            }

            if (!StringUtil.isEmptyOrNull(aliResult.getResult().getAliMchntNo())) {
                //入驻成功
                alipayMchntInfo.setAlipayMchntNo(aliResult.getResult().getAliMchntNo());
                alipayMchntInfo.setAlipayEntryFlag("01");
                alipayMchntInfo.setLastEntryTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                    alipayMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
                }
            } else {
                //入驻失败
                alipayMchntInfo.setAlipayEntryFlag("02");
            }

            //更新商户支付宝信息表与历史数据表
            alipayMchntInfo.setAlipayMchntNo(aliResult.getResult().getAliMchntNo());
            if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                alipayMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
            }
            BizHistory bizHistory = new BizHistory();
            BeanUtil.beanCopy(alipayMchntInfo, bizHistory);
            bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
            bizHistory.setMchntName(mchntBaseInfo.getMchntName());
            bizHistory.setRealId(alipayMchntInfo.getId());
            bizHistory.setOprTableEname(TableNameConstant.T_B_ALPAY_MCHNT_INFO);
            //修改
            bizHistory.setBizOpr("U");
            bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
            String modifyAliMchntInfoJson = JSONObject.toJSONString(alipayMchntInfo);

            //将正式表的商户信息存入历史表 bizData
            InitParamUtil.initBizHistoryData(bizHistory, modifyAliMchntInfoJson);
            modifyAliMchntInfoData(alipayMchntInfo, bizHistory);
        } else {
            List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg aliChannelCallCfg = channelCallCfgList.get(1);

            //调用支付宝进行商户修改
            AliCreateMerchantRequest aliCreateMerchantRequest = initAliCreateMerchantRequest(instMchntRegisterRequest, sysSeqNum, mchntBaseInfo, alipayMchntInfo, aliChannelCallCfg.getCallChannelId(), aliChannelCallCfg);
            AliUpdateMerchantRequest aliUpdateMerchantRequest = new AliUpdateMerchantRequest();
            //数据一致
            BeanUtil.beanCopy(aliCreateMerchantRequest, aliUpdateMerchantRequest);
            aliUpdateMerchantRequest.setHeader(aliCreateMerchantRequest.getHeader());
            DubboService dubboService = new DubboServiceImpl();
            Object object = dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliUpdateMerchant", aliUpdateMerchantRequest);
            cc.rengu.igas.channel.alipay.facade.result.Result<AliUpdateMerchantResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliUpdateMerchantResponse>) object;
            if (null == aliResult) {
                rglog.error("支付宝商户信息修改, 当前支付宝商户信息入驻失败，instId:{},mchntNo:{},aliResult:{}.",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else if (!aliResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
                rglog.error("支付宝商户信息修改, 当前支付宝商户信息入驻失败，instId:{},mchntNo:{},aliResult:{}.",
                        alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
                throw new BizException(aliResult.getResult().getRespCode(), aliResult.getResult().getRespDesc());
            }

            //更新商户支付宝信息表与历史数据表
            if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                alipayMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
            }
            BizHistory bizHistory = new BizHistory();
            BeanUtil.beanCopy(alipayMchntInfo, bizHistory);
            bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
            bizHistory.setMchntName(mchntBaseInfo.getMchntName());
            bizHistory.setRealId(alipayMchntInfo.getId());
            bizHistory.setOprTableEname(TableNameConstant.T_B_ALPAY_MCHNT_INFO);
            bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
            //修改
            bizHistory.setBizOpr("U");
            String modifyAliMchntInfoJson = JSONObject.toJSONString(alipayMchntInfo);

            //将正式表的商户信息存入历史表 bizData
            InitParamUtil.initBizHistoryData(bizHistory, modifyAliMchntInfoJson);
            modifyAliMchntInfoData(alipayMchntInfo, bizHistory);
        }
    }

    private void wechatCreateMchntOrUpdateMchnt(InstMchntRegisterRequest instMchntRegisterRequest, String sysSeqNum, PlatMchntInfo platMchntInfo) throws Exception {
        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(
                instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
        if (null == wechatMchntInfo) {
            rglog.error("微信商户信息查询失败！instId:<{}>,mchntNo:<{}>",
                    instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
            throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
        }

        //判断当前状态是否允许修改
        if (!ChangeNextStatusUtil.isAllowModifyData(wechatMchntInfo.getAuditStatus())) {
            rglog.error("第三方机构商户进件处理失败, 当前微信商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                    wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getAuditStatus());
            throw new BizException(RespCodeEnum.MANAGE_OPERATE_ERROR.getRespCode(), RespCodeEnum.MANAGE_OPERATE_ERROR.getRespDesc());
        }

        WechatIntegration wechatIntegration = new WechatIntegration();

        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(wechatMchntInfo.getInstId(), BompAppParamConstant.SYS_PARAM_CHANNEL, BompAppParamConstant.WECHAT_PAY_CHANNEL_ID);
        if (null == sysParam) {
            rglog.error("机构<{}>未配置微信支付通道选择", wechatMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        //入驻
        if ("0".equals(instMchntRegisterRequest.getOprType())) {
            //获取通道服务调用配置-微信
            //获取通道服务调用配置-银联微信
            List<ChannelCallCfg> wxNumMapList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg wxChannelCallCfg = wxNumMapList.get(0);

            if (StringUtil.isEmptyOrNull(wechatMchntInfo.getWechatMchntNo())) {
                WechatCreateMerchantRequest wechatCreateMerchantRequest = initWechatCreateMerchantRequestByPlatMchntNo(instMchntRegisterRequest, sysSeqNum, wechatMchntInfo, wxChannelCallCfg, sysParam.getParamValue(), platMchntInfo);
                DubboService dubboService = new DubboServiceImpl();
                Result<WechatCreateMerchantResponse> wechatResult = (Result<WechatCreateMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "createMerchant", wechatCreateMerchantRequest);
                if (null == wechatResult || !wechatResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(wechatResult.getResult().getRespCode())) {
                    rglog.error("微信商户入驻, 当前微信商户信息入驻失败，instId:{},mchntNo:{},wechatResult:{}",
                            wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                    throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                } else {
                    WechatCreateMerchantResponse wechatCreateMerchantResponse = (WechatCreateMerchantResponse) wechatResult.getResult();
                    if (DubboMethodConstant.DUBBO_SUCCESS.equals(wechatCreateMerchantResponse.getRespCode()) && !StringUtil.isEmptyOrNull(wechatCreateMerchantResponse.getThirdMchntNo())) {
                        //入驻成功
                        wechatMchntInfo.setWechatMchntNo(wechatCreateMerchantResponse.getThirdMchntNo());
                        wechatMchntInfo.setWechatEntryFlag("01");
                        wechatMchntInfo.setLastEntryTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                        if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                            wechatMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
                        }
                        updateWechatMchntInfoByPlatMchntInfo(platMchntInfo, wechatMchntInfo);
                    } else {
                        wechatMchntInfo.setWechatEntryFlag("02");
                        updateWechatMchntInfoByPlatMchntInfo(platMchntInfo, wechatMchntInfo);

                        rglog.error("微信商户入驻, 当前微信商户信息入驻失败，instId:{},mchntNo:{},wechatResult:{}",
                                wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                        throw new BizException(wechatCreateMerchantResponse.getRespCode(), wechatCreateMerchantResponse.getRespDesc());
                    }
                }
            }

            //设置支付目录
            boolean addJsapiPath = wechatIntegration.addJsapiPath(wechatMchntInfo, sysSeqNum, txnInfo);

            if (addJsapiPath) {
                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getWechatAppid())) {
                    wechatMchntInfo.setAppId(instMchntRegisterRequest.getWechatAppid());
                }
                updateWechatMchntInfoByPlatMchntInfo(platMchntInfo, wechatMchntInfo);
            }

            //绑定APPID
            boolean bindAppid = wechatIntegration.bindAppid(wechatMchntInfo, sysSeqNum, txnInfo);
            if (bindAppid) {
                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getWechatAppid())) {
                    wechatMchntInfo.setAppId(instMchntRegisterRequest.getWechatAppid());
                }


                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getWechatMiniAppid())) {
                    wechatMchntInfo.setMiniAppId(instMchntRegisterRequest.getWechatMiniAppid());
                }
                updateWechatMchntInfoByPlatMchntInfo(platMchntInfo, wechatMchntInfo);
            }

            if (!addJsapiPath || !bindAppid) {
                rglog.error("绑定微信APPid或设置微信支付目录失败！");
                throw new BizException(RespCodeEnum.WECHAT_SET_PATH_OR_APPID_ERROR.getRespCode(), RespCodeEnum.WECHAT_SET_PATH_OR_APPID_ERROR.getRespDesc());
            }


        } else {
            //修改
            //获取通道服务调用配置-银联微信
            List<ChannelCallCfg> wxNumMapList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg wxChannelCallCfg = wxNumMapList.get(1);

            WechatUpdateMerchantRequest wechatUpdateMerchantRequest = new WechatUpdateMerchantRequest();
            BeanUtil.beanCopy(wechatMchntInfo, wechatUpdateMerchantRequest);
            Header header = new Header();
            header.setChanlId(ChannelEnum.BOMP.getChannelType());
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setTxnNum(wxChannelCallCfg.getCallChannelTxn());
            header.setVersion("1.0.0");
            String channelSeqNum = sysSeqNum + wxChannelCallCfg.getCallIndex();
            header.setTraceNo(channelSeqNum);
            header.setTransTime(DateUtil.getCurrentTime());
            header.setTransDate(DateUtil.getCurrentDate());
            wechatUpdateMerchantRequest.setHeader(header);
            wechatUpdateMerchantRequest.setChannelId(sysParam.getParamValue());
            //商户产品信息表的微信支付通道编码
            wechatUpdateMerchantRequest.setChannelType(instMchntRegisterRequest.getChannelType());
            wechatUpdateMerchantRequest.setThirdMchntNo(wechatMchntInfo.getWechatMchntNo());
            wechatUpdateMerchantRequest.setMchntAbbr(platMchntInfo.getPlatMchntName());
            wechatUpdateMerchantRequest.setServicePhone(platMchntInfo.getContactPhone());
            DubboService dubboService = new DubboServiceImpl();
            Result<WechatUpdateMerchantResponse> wechatResult = (Result<WechatUpdateMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "updateMerchant", wechatUpdateMerchantRequest);
            if (null == wechatResult) {
                rglog.error("第三方机构商户信息修改, 当前微信商户信息修改失败，instId:{},mchntNo:{}, wechatResult:{}",
                        wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else {
                WechatUpdateMerchantResponse wechatUpdateMerchantResponse = (WechatUpdateMerchantResponse) wechatResult.getResult();
                if (DubboMethodConstant.DUBBO_SUCCESS.equals(wechatUpdateMerchantResponse.getRespCode())) {
                    if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                        wechatMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
                    }
                } else {
                    rglog.error("第三方机构商户信息修改, 当前微信商户信息修改失败，instId:{},mchntNo:{}, wechatResult:{}",
                            wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                    throw new BizException(wechatUpdateMerchantResponse.getRespCode(), wechatUpdateMerchantResponse.getRespDesc());
                }
            }

            updateWechatMchntInfoByPlatMchntInfo(platMchntInfo, wechatMchntInfo);
        }
    }


    private void wechatCreateMchntOrUpdateMchnt(InstMchntRegisterRequest instMchntRegisterRequest, String sysSeqNum, MchntBaseInfo mchntBaseInfo) throws Exception {
        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(
                instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
        if (null == wechatMchntInfo) {
            rglog.error("微信商户信息查询失败！instId:<{}>,mchntNo:<{}>",
                    instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());
            throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
        }

        //判断当前状态是否允许修改
        if (!ChangeNextStatusUtil.isAllowModifyData(wechatMchntInfo.getAuditStatus())) {
            rglog.error("第三方机构商户进件处理失败, 当前微信商户信息待审核，不允许进行进件操作，instId:{},mchntNo:{},当前商户审核状态:{}",
                    wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getAuditStatus());
            throw new BizException(RespCodeEnum.MCHNT_REGISTER_EOOOR.getRespCode(), "当前商户信息待审核");
        }

        WechatIntegration wechatIntegration = new WechatIntegration();

        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(wechatMchntInfo.getInstId(), BompAppParamConstant.SYS_PARAM_CHANNEL, BompAppParamConstant.WECHAT_PAY_CHANNEL_ID);
        if (null == sysParam) {
            rglog.error("机构<{}>未配置微信支付通道选择", wechatMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        //入驻
        if ("0".equals(instMchntRegisterRequest.getOprType())) {
            //获取通道服务调用配置-微信
            //获取通道服务调用配置-银联微信
            List<ChannelCallCfg> wxNumMapList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg wxChannelCallCfg = wxNumMapList.get(0);

            if (StringUtil.isEmptyOrNull(wechatMchntInfo.getWechatMchntNo())) {
                WechatCreateMerchantRequest wechatCreateMerchantRequest = initWechatCreateMerchantRequest(instMchntRegisterRequest, sysSeqNum, wechatMchntInfo, wxChannelCallCfg, sysParam.getParamValue(), mchntBaseInfo);
                DubboService dubboService = new DubboServiceImpl();
                Result<WechatCreateMerchantResponse> wechatResult = (Result<WechatCreateMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "createMerchant", wechatCreateMerchantRequest);
                if (null == wechatResult || !wechatResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(wechatResult.getResult().getRespCode())) {
                    rglog.error("微信商户入驻, 当前微信商户信息入驻失败，instId:{},mchntNo:{},wechatResult:{}",
                            wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                    throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                } else {
                    WechatCreateMerchantResponse wechatCreateMerchantResponse = (WechatCreateMerchantResponse) wechatResult.getResult();
                    if (DubboMethodConstant.DUBBO_SUCCESS.equals(wechatCreateMerchantResponse.getRespCode()) && !StringUtil.isEmptyOrNull(wechatCreateMerchantResponse.getThirdMchntNo())) {
                        //入驻成功
                        wechatMchntInfo.setWechatMchntNo(wechatCreateMerchantResponse.getThirdMchntNo());
                        wechatMchntInfo.setWechatEntryFlag("01");
                        wechatMchntInfo.setLastEntryTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                        //将独立进件的微信商户号存到remark1中
                        wechatMchntInfo.setRemark1(wechatCreateMerchantResponse.getThirdMchntNo());
                        if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                            wechatMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
                        }
                        updateWechatMchntInfo(mchntBaseInfo, wechatMchntInfo);
                    } else {
                        //入驻失败
                        wechatMchntInfo.setWechatEntryFlag("02");
                        updateWechatMchntInfo(mchntBaseInfo, wechatMchntInfo);
                        rglog.error("商户mchntNo:{}信息微信入驻失败,wechatResult:{}", wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                        throw new BizException(wechatCreateMerchantResponse.getRespCode(), wechatCreateMerchantResponse.getRespDesc());
                    }
                }
            }

            //绑定支付目录，绑定APPID

            boolean addJsapiPath = true;
            boolean bindAppid = true;

            //设置支付目录与绑定APPid
            addJsapiPath = wechatIntegration.addJsapiPath(wechatMchntInfo, sysSeqNum, txnInfo);
            if (addJsapiPath) {

                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getWechatAppid())) {
                    wechatMchntInfo.setAppId(instMchntRegisterRequest.getWechatAppid());
                }

                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getWechatMiniAppid())) {
                    wechatMchntInfo.setMiniAppId(instMchntRegisterRequest.getWechatMiniAppid());
                }
                updateWechatMchntInfo(mchntBaseInfo, wechatMchntInfo);
            }

            //绑定APPID
            bindAppid = wechatIntegration.bindAppid(wechatMchntInfo, sysSeqNum, txnInfo);
            if (bindAppid) {
                if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getWechatMiniAppid())) {
                    wechatMchntInfo.setMiniAppId(instMchntRegisterRequest.getWechatMiniAppid());
                }
                updateWechatMchntInfo(mchntBaseInfo, wechatMchntInfo);
            }

            if (!addJsapiPath || !bindAppid) {
                rglog.error("绑定微信APPid或设置微信支付目录失败！");
                throw new BizException(RespCodeEnum.WECHAT_SET_PATH_OR_APPID_ERROR.getRespCode(), RespCodeEnum.WECHAT_SET_PATH_OR_APPID_ERROR.getRespDesc());
            }

        } else {
            //修改
            //获取通道服务调用配置-银联微信
            List<ChannelCallCfg> wxNumMapList = txnInfo.getChannelCallCfgList().stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType())).sorted(Comparator.comparing(ChannelCallCfg::getCallIndex)).collect(Collectors.toList());
            ChannelCallCfg wxChannelCallCfg = wxNumMapList.get(1);

            WechatUpdateMerchantRequest wechatUpdateMerchantRequest = new WechatUpdateMerchantRequest();
            BeanUtil.beanCopy(wechatMchntInfo, wechatUpdateMerchantRequest);
            Header header = new Header();
            header.setChanlId(ChannelEnum.BOMP.getChannelType());
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setTxnNum(wxChannelCallCfg.getCallChannelTxn());
            header.setVersion("1.0.0");
            String channelSeqNum = sysSeqNum + wxChannelCallCfg.getCallIndex();
            header.setTraceNo(channelSeqNum);
            header.setTransTime(DateUtil.getCurrentTime());
            header.setTransDate(DateUtil.getCurrentDate());
            wechatUpdateMerchantRequest.setHeader(header);
            wechatUpdateMerchantRequest.setChannelId(sysParam.getParamValue());
            //商户产品信息表的微信支付通道编码
            wechatUpdateMerchantRequest.setChannelType(instMchntRegisterRequest.getChannelType());
            wechatUpdateMerchantRequest.setThirdMchntNo(wechatMchntInfo.getWechatMchntNo());
            wechatUpdateMerchantRequest.setMchntAbbr(mchntBaseInfo.getMchntSimpleName());
            wechatUpdateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
            DubboService dubboService = new DubboServiceImpl();
            Result<WechatUpdateMerchantResponse> wechatResult = (Result<WechatUpdateMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "updateMerchant", wechatUpdateMerchantRequest);
            if (null == wechatResult) {
                rglog.error("第三方机构商户信息修改, 当前微信商户信息修改失败，instId:{},mchntNo:{}, wechatResult:{}",
                        wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else {
                WechatUpdateMerchantResponse wechatUpdateMerchantResponse = (WechatUpdateMerchantResponse) wechatResult.getResult();
                if (DubboMethodConstant.DUBBO_SUCCESS.equals(wechatUpdateMerchantResponse.getRespCode())) {
                    if (!StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
                        wechatMchntInfo.setDstChannelType(instMchntRegisterRequest.getChannelType());
                    }
                    updateWechatMchntInfo(mchntBaseInfo, wechatMchntInfo);
                } else {
                    rglog.error("第三方机构商户信息修改, 当前微信商户信息修改失败，instId:{},mchntNo:{}, wechatResult:{}",
                            wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), JSONObject.toJSONString(wechatResult));
                    throw new BizException(wechatUpdateMerchantResponse.getRespCode(), wechatUpdateMerchantResponse.getRespDesc());
                }
            }
        }
    }

    private void updateWechatMchntInfo(MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo) throws Exception {
        BizTmp bizTmp = InitParamUtil.initBizTmp(mchntBaseInfo, wechatMchntInfo.getId(), JSONObject.toJSONString(wechatMchntInfo),
                TableNameConstant.T_B_WECHAT_MCHNT_INFO, wechatMchntInfo.getAuditStatus(), AppParamConstant.NO, AppParamConstant.NO);

        //更新商户微信信息表与历史数据表
        BizHistory bizHistory = new BizHistory();
        BeanUtil.beanCopy(wechatMchntInfo, bizHistory);
        bizHistory.setManageInstId(mchntBaseInfo.getSignInstId());
        bizHistory.setMchntName(mchntBaseInfo.getMchntName());
        bizHistory.setRealId(wechatMchntInfo.getId());
        bizHistory.setOprTableEname(TableNameConstant.T_B_WECHAT_MCHNT_INFO);
        //修改
        bizHistory.setBizOpr("U");
        bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
        bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
        String modifyWechatMchntInfoJson = JSONObject.toJSONString(wechatMchntInfo);

        //将正式表的商户信息存入历史表 bizData
        InitParamUtil.initBizHistoryData(bizHistory, modifyWechatMchntInfoJson);
        modifyWechatMchntInfoData(wechatMchntInfo, bizTmp, bizHistory);
    }

    private void updateWechatMchntInfoByPlatMchntInfo(PlatMchntInfo platMchntInfo, WechatMchntInfo wechatMchntInfo) throws Exception {
        //更新商户微信信息表与历史数据表
        BizHistory bizHistory = new BizHistory();
        BeanUtil.beanCopy(wechatMchntInfo, bizHistory);
        bizHistory.setManageInstId(platMchntInfo.getManageInstId());
        bizHistory.setMchntName(platMchntInfo.getPlatMchntName());
        bizHistory.setRealId(wechatMchntInfo.getId());
        bizHistory.setOprTableEname(TableNameConstant.T_B_WECHAT_MCHNT_INFO);
        //修改
        bizHistory.setBizOpr("U");
        bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
        bizHistory.setCreateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
        String modifyWechatMchntInfoJson = JSONObject.toJSONString(wechatMchntInfo);

        //将正式表的商户信息存入历史表 bizData
        InitParamUtil.initBizHistoryData(bizHistory, modifyWechatMchntInfoJson);
        modifyWechatMchntInfoData(wechatMchntInfo, null, bizHistory);
    }


    private WechatCreateMerchantRequest initWechatCreateMerchantRequest(InstMchntRegisterRequest instMchntRegisterRequest,
                                                                        String sysSeqNum,
                                                                        WechatMchntInfo wechatMchntInfo,
                                                                        ChannelCallCfg channelCallCfg,
                                                                        String channelId,
                                                                        MchntBaseInfo mchntBaseInfo) throws Exception {
        String instId = instMchntRegisterRequest.getHeader().getInstId();
        String mchntNo = instMchntRegisterRequest.getMchntNo();

        WechatCreateMerchantRequest wechatCreateMerchantRequest = new WechatCreateMerchantRequest();
        BeanUtil.beanCopy(wechatMchntInfo, wechatCreateMerchantRequest);
        Header header = new Header();
        BeanUtil.beanCopy(instMchntRegisterRequest.getHeader(), header);
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        wechatCreateMerchantRequest.setHeader(header);
        wechatCreateMerchantRequest.setChannelId(channelId);
        //商户产品信息表的微信支付通道编码
        if (StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
            wechatCreateMerchantRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        } else {
            wechatCreateMerchantRequest.setChannelType(instMchntRegisterRequest.getChannelType());
        }
        wechatCreateMerchantRequest.setMchntNo(mchntBaseInfo.getMchntNo());
        wechatCreateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
        wechatCreateMerchantRequest.setContact(mchntBaseInfo.getMchntContactName());

        //商户经营类目  微信二级类目
        wechatCreateMerchantRequest.setBusiness(wechatMchntInfo.getWechatSecondCategory());
        wechatCreateMerchantRequest.setMchntName(mchntBaseInfo.getMchntName());
        wechatCreateMerchantRequest.setMchntAbbr(mchntBaseInfo.getMchntSimpleName());
        //可填固话
        wechatCreateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
        wechatCreateMerchantRequest.setContact(mchntBaseInfo.getMchntContactName());
        //手机号，不能填固话
        wechatCreateMerchantRequest.setContactPhone(mchntBaseInfo.getMchntContactMobile());
        wechatCreateMerchantRequest.setContactEmail(mchntBaseInfo.getMchntContactEmail());

        return wechatCreateMerchantRequest;
    }


    private WechatCreateMerchantRequest initWechatCreateMerchantRequestByPlatMchntNo(InstMchntRegisterRequest instMchntRegisterRequest,
                                                                                     String sysSeqNum,
                                                                                     WechatMchntInfo wechatMchntInfo,
                                                                                     ChannelCallCfg channelCallCfg,
                                                                                     String channelId,
                                                                                     PlatMchntInfo platMchntInfo) throws Exception {
        String instId = instMchntRegisterRequest.getHeader().getInstId();
        String mchntNo = instMchntRegisterRequest.getMchntNo();

        WechatCreateMerchantRequest wechatCreateMerchantRequest = new WechatCreateMerchantRequest();
        BeanUtil.beanCopy(wechatMchntInfo, wechatCreateMerchantRequest);
        Header header = new Header();
        BeanUtil.beanCopy(instMchntRegisterRequest.getHeader(), header);
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        wechatCreateMerchantRequest.setHeader(header);
        wechatCreateMerchantRequest.setChannelId(channelId);
        //商户产品信息表的微信支付通道编码
        if (StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
            wechatCreateMerchantRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        } else {
            wechatCreateMerchantRequest.setChannelType(instMchntRegisterRequest.getChannelType());
        }
        wechatCreateMerchantRequest.setMchntNo(platMchntInfo.getPlatMchntNo());
        wechatCreateMerchantRequest.setServicePhone(platMchntInfo.getContactPhone());
        wechatCreateMerchantRequest.setContact(platMchntInfo.getContactName());
        wechatCreateMerchantRequest.setBusiness(wechatMchntInfo.getWechatSecondCategory());

        wechatCreateMerchantRequest.setMchntName(platMchntInfo.getPlatMchntName());
        wechatCreateMerchantRequest.setMchntAbbr(platMchntInfo.getPlatMchntName());
        wechatCreateMerchantRequest.setServicePhone(platMchntInfo.getContactPhone());
        wechatCreateMerchantRequest.setContact(platMchntInfo.getContactName());
        wechatCreateMerchantRequest.setContactPhone(platMchntInfo.getContactPhone());
        wechatCreateMerchantRequest.setContactEmail(platMchntInfo.getContactEmaiil());

        return wechatCreateMerchantRequest;
    }


    private AliCreateMerchantRequest initAliCreateMerchantRequestByPlatMchntInfo(InstMchntRegisterRequest instMchntRegisterRequest,
                                                                                 String sysSeqNum,
                                                                                 PlatMchntInfo platMchntInfo,
                                                                                 AlipayMchntInfo alipayMchntInfo,
                                                                                 String channelId,
                                                                                 ChannelCallCfg channelCallCfg) {
        String instId = instMchntRegisterRequest.getHeader().getInstId();
        String mchntNo = instMchntRegisterRequest.getMchntNo();

        AliCreateMerchantRequest aliCreateMerchantRequest = new AliCreateMerchantRequest();
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        BeanUtil.beanCopy(alipayMchntInfo, aliCreateMerchantRequest);
        BeanUtil.beanCopy(instMchntRegisterRequest.getHeader(), header);
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        aliCreateMerchantRequest.setHeader(header);
        aliCreateMerchantRequest.setMchntName(platMchntInfo.getPlatMchntName());
        //商户MCC
        aliCreateMerchantRequest.setMchntType(platMchntInfo.getIndustryType());
        aliCreateMerchantRequest.setMchntAbbr(platMchntInfo.getPlatMchntName());
        aliCreateMerchantRequest.setAlipayCategory(alipayMchntInfo.getAlipayCategory());
        //客服电话
        aliCreateMerchantRequest.setServicePhone(platMchntInfo.getContactPhone());
        aliCreateMerchantRequest.setChannelId(channelId);

        if (StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
            aliCreateMerchantRequest.setChannelType(alipayMchntInfo.getDstChannelType());
        } else {
            aliCreateMerchantRequest.setChannelType(instMchntRegisterRequest.getChannelType());
        }

        // 联系人信息
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setContact(platMchntInfo.getArtifName());
        contactInfo.setContactTelephone(platMchntInfo.getArtifMobile());
        contactInfo.setContactPhone(platMchntInfo.getArtifMobile());
        contactInfo.setContactEmail(platMchntInfo.getArtifEmail());
        //商户关键联系人-06
        contactInfo.setContactJobType("06");
        //法人
        contactInfo.setContactType("LEGAL_PERSON");
        contactInfo.setCertId(platMchntInfo.getArtifCertNo());
        aliCreateMerchantRequest.setContactInfoList(Collections.singletonList(contactInfo));

        //地址信息
//        AddressInfo addressInfo = new AddressInfo();
//        addressInfo.setCityCode(platMchntInfo.getRegisterCity());
//        addressInfo.setDistrictCode(platMchntInfo.getRegisterArea());
//        addressInfo.setAddress(platMchntInfo.getPlatMchntAddress());
//        addressInfo.setProvinceCode(platMchntInfo.getRegisterProvince());
//        aliCreateMerchantRequest.setAddressInfoList(Collections.singletonList(addressInfo));

        List<BankCardInfo> bankCardInfoList = new ArrayList<>();
        BankCardInfo bankCardInfo = new BankCardInfo();
        bankCardInfo.setAcctNo(platMchntInfo.getSettleAcctNo());
        bankCardInfo.setAcctName(platMchntInfo.getSettleAcctName());
        bankCardInfo.setBankName(platMchntInfo.getAcctOpenBankName());
        bankCardInfoList.add(bankCardInfo);

        aliCreateMerchantRequest.setBankCardInfoList(bankCardInfoList);
        return aliCreateMerchantRequest;
    }


    private AliCreateMerchantRequest initAliCreateMerchantRequest(InstMchntRegisterRequest instMchntRegisterRequest,
                                                                  String sysSeqNum,
                                                                  MchntBaseInfo mchntBaseInfo,
                                                                  AlipayMchntInfo alipayMchntInfo,
                                                                  String channelId,
                                                                  ChannelCallCfg channelCallCfg) throws Exception {
        String instId = instMchntRegisterRequest.getHeader().getInstId();
        String mchntNo = instMchntRegisterRequest.getMchntNo();

        MchntSignInfoMapper mchntSignInfoMapper = new MchntSignInfoMapperImpl();
        MchntSignInfo mchntSignInfo = mchntSignInfoMapper.selectMchntSignInfoByPrimaryKey(instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());

        MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
        List<MchntAcctInfo> mchntAcctInfoList = mchntAcctInfoMapper.selectMchntAcctInfoByMchntNo(instMchntRegisterRequest.getHeader().getInstId(), instMchntRegisterRequest.getMchntNo());

        AliCreateMerchantRequest aliCreateMerchantRequest = new AliCreateMerchantRequest();
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        BeanUtil.beanCopy(alipayMchntInfo, aliCreateMerchantRequest);
        BeanUtil.beanCopy(instMchntRegisterRequest.getHeader(), header);
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + channelCallCfg.getCallIndex();
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        aliCreateMerchantRequest.setHeader(header);
        aliCreateMerchantRequest.setMchntName(mchntBaseInfo.getMchntName());
        //商户MCC
        aliCreateMerchantRequest.setMchntType(mchntBaseInfo.getIndustryType());
        aliCreateMerchantRequest.setMchntAbbr(mchntBaseInfo.getMchntSimpleName());
        //客服电话
        aliCreateMerchantRequest.setServicePhone(mchntBaseInfo.getMchntContactPhone());
        aliCreateMerchantRequest.setChannelId(channelId);

        if (StringUtil.isEmptyOrNull(instMchntRegisterRequest.getChannelType())) {
            aliCreateMerchantRequest.setChannelType(alipayMchntInfo.getDstChannelType());
        } else {
            aliCreateMerchantRequest.setChannelType(instMchntRegisterRequest.getChannelType());
        }

        // 联系人信息
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setContact(mchntSignInfo.getArtifName());
        contactInfo.setContactTelephone(mchntSignInfo.getArtifMobile());
        contactInfo.setContactPhone(mchntSignInfo.getArtifMobile());
        contactInfo.setContactEmail(mchntSignInfo.getArtifEmail());
        contactInfo.setContactJobType("06");
        contactInfo.setContactType("LEGAL_PERSON");
        contactInfo.setCertId(mchntSignInfo.getArtifCertNo());
        aliCreateMerchantRequest.setContactInfoList(Collections.singletonList(contactInfo));

        //签约相关信息
        SignInfo signInfo = new SignInfo();
        signInfo.setBusinessLicenseNo(mchntSignInfo.getLicenceCode());
        /* 获取商户签约影像信息 */
        MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
        List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNo(instId, mchntNo);
        if (null != mchntImageInfoList && !mchntImageInfoList.isEmpty()) {
            SysParamMapper sysParamMapper = new SysParamMapperImpl();
            SysParam localUrlPrefix = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.IMAGE_STORAGE_PARAM_TYPE, BompAppParamConstant.LOCAL_IMAGE_STORAGE_PATH);
            if (null == localUrlPrefix) {
                rglog.error("查询系统参数表，根据机构获取图片存储服务器路径为空！InstId：<{}>!", instId);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            //共享存储文件夹路径
            String shareStorageUrl = localUrlPrefix.getParamValue().trim().endsWith(File.separator) ? localUrlPrefix.getParamValue().trim() : localUrlPrefix.getParamValue().trim() + File.separator;
            //营销执照图片
            Optional<MchntImageInfo> mchntImageInfoOpt = mchntImageInfoList.stream().filter(item -> item.getImageType().equals(ImageTypeEnum.BUSINESS_LICENSE.getImageType())).findFirst();
            mchntImageInfoOpt.ifPresent(mchntImageInfo -> signInfo.setBusinessLicensePic(shareStorageUrl + mchntImageInfo.getImageLocalPath()));
            //店铺内景照
            mchntImageInfoOpt = mchntImageInfoList.stream().filter(item -> item.getImageType().equals(ImageTypeEnum.BUSINESS_SITE_PHOTO.getImageType())).findFirst();
            mchntImageInfoOpt.ifPresent(mchntImageInfo -> signInfo.setShopScenePic(shareStorageUrl + mchntImageInfo.getImageLocalPath()));
            //店铺门头照
            mchntImageInfoOpt = mchntImageInfoList.stream().filter(item -> item.getImageType().equals(ImageTypeEnum.SHOP_FRONT_PHOTO.getImageType())).findFirst();
            mchntImageInfoOpt.ifPresent(mchntImageInfo -> signInfo.setShopSignBoardPic(shareStorageUrl + mchntImageInfo.getImageLocalPath()));
        }
        aliCreateMerchantRequest.setSignInfo(signInfo);

        //地址信息
        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setProvinceCode(mchntBaseInfo.getMchntBizProvince());
        addressInfo.setCityCode(mchntBaseInfo.getMchntBizCity());
        addressInfo.setDistrictCode(mchntBaseInfo.getMchntBizRegion());
        addressInfo.setAddress(mchntBaseInfo.getMchntBizAddr());
        aliCreateMerchantRequest.setAddressInfoList(Collections.singletonList(addressInfo));

        List<BankCardInfo> bankCardInfoList = new ArrayList<>();
        mchntAcctInfoList.forEach(item -> convertBankCardInfo(item, bankCardInfoList));
        aliCreateMerchantRequest.setBankCardInfoList(bankCardInfoList);
        return aliCreateMerchantRequest;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InstMchntRegisterResponse instMchntRegisterResponse = (InstMchntRegisterResponse) bizResponse.getResult();
        ConvertUtil.convertInput(instMchntRegisterResponse);
    }

    private void modifyAliMchntInfoData(AlipayMchntInfo alipayMchntInfo, BizHistory bizHistory) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
        int dbResult = alipayMchntInfoMapper.updateAlipayMchntInfoById(alipayMchntInfo);
        if (dbResult != 0) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("支付宝商户进件-修改支付宝商户信息表失败！id:{},mchnt:{},instId:{}",
                    alipayMchntInfo.getId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        if (null != bizHistory) {
            BizHistoryMapper bizHistoryMapper = new BizHistoryMapperImpl();
            bizHistory.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            dbResult = bizHistoryMapper.insertBompBizHistory(bizHistory);
            if (dbResult != 0) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("支付宝商户进件-新增业务历史表记录失败！realId:{},manageInstId:{},instId:{}",
                        bizHistory.getRealId(), bizHistory.getManageInstId(), bizHistory.getInstId());
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }

        dbsUtil.dbsEndTransaction(true);
    }


    private void modifyWechatMchntInfoData(WechatMchntInfo wechatMchntInfo, BizTmp bizTmp, BizHistory bizHistory) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        /** 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        int dbResult = wechatMchntInfoMapper.updateWechatMchntInfoById(wechatMchntInfo);
        if (dbResult != 0) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("微信商户进件-修改微信商户信息表失败！id:{},mchnt:{},instId:{}",
                    wechatMchntInfo.getId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        if (null != bizTmp) {
            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            dbResult = bizTmpMapper.updateBizTmpByRealId(bizTmp);
            if (dbResult != 0) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("微信商户进件-修改临时表-微信商户信息表失败！id:{},mchntNo:{},instId:{}",
                        wechatMchntInfo.getId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getInstId());
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }

        if (null != bizHistory) {
            BizHistoryMapper bizHistoryMapper = new BizHistoryMapperImpl();
            dbResult = bizHistoryMapper.insertBompBizHistory(bizHistory);
            if (dbResult != 0) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("微信商户进件-新增业务历史表记录失败！realId:{},manageInstId:{},instId:{}",
                        bizHistory.getRealId(), bizHistory.getManageInstId(), bizHistory.getInstId());
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }

        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param mchntAcctInfo    数据库表结构对象
     * @param bankCardInfoList 接口对象
     */
    private void convertBankCardInfo(MchntAcctInfo mchntAcctInfo, List<BankCardInfo> bankCardInfoList) {
        BankCardInfo bankCardInfo = new BankCardInfo();
        bankCardInfo.setAcctNo(mchntAcctInfo.getSettleAcctNo());
        bankCardInfo.setAcctName(mchntAcctInfo.getSettleAcctName());
        bankCardInfo.setBankName(mchntAcctInfo.getAcctOpenBankName());
        bankCardInfoList.add(bankCardInfo);
    }

    /**
     * 获取支付宝商户应用授权URL
     *
     * @param instMchntRegisterRequest 请求对象
     * @param alipayMchntInfo          支付宝商户信息
     * @return 商户应用授权URL
     * @throws Exception 异常
     */
    private String getAlipayMchntAppAuth(InstMchntRegisterRequest instMchntRegisterRequest, AlipayMchntInfo alipayMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        DubboService dubboService = new DubboServiceImpl();
        AliGetAppAuthCodeRequest aliGetAppAuthCodeRequest = new AliGetAppAuthCodeRequest();
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        BeanUtil.beanCopy(alipayMchntInfo, aliGetAppAuthCodeRequest);
        BeanUtil.beanCopy(instMchntRegisterRequest.getHeader(), header);
        header.setChanlId(ChannelEnum.BOMP.getChannelType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setTxnNum("agetappauthcode");
        header.setVersion("1.0.0");
        String channelSeqNum = sysSeqNum + RandomUtil.randomNumStr(4);
        header.setTraceNo(channelSeqNum);
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        aliGetAppAuthCodeRequest.setHeader(header);
        aliGetAppAuthCodeRequest.setChannelId(ChannelEnum.ALIP.getChannelType());
        aliGetAppAuthCodeRequest.setChannelType(alipayMchntInfo.getDstChannelType());
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(alipayMchntInfo.getInstId(), "aliPay", "mchntAppAuthRedirectUrl");
        if (null == sysParam || StringUtil.isEmptyOrNull(sysParam.getParamValue())) {
            rglog.error("未配置支付宝商户应用授权回调地址!");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        aliGetAppAuthCodeRequest.setRedirectUrl(sysParam.getParamValue());
        aliGetAppAuthCodeRequest.setState(alipayMchntInfo.getMchntNo());
        cc.rengu.igas.channel.alipay.facade.result.Result<AliGetAppAuthCodeResponse> aliResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliGetAppAuthCodeResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "getAppAuthCode", aliGetAppAuthCodeRequest);
        if (null == aliResult) {
            rglog.error("获取支付宝商户应用授权URL失败，instId:{},mchntNo:{},aliResult:{}.",
                    alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        } else if (!aliResult.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(aliResult.getResult().getRespCode())) {
            rglog.error("获取支付宝商户应用授权URL失败，instId:{},mchntNo:{},aliResult:{}.",
                    alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), JSONObject.toJSONString(aliResult));
            throw new BizException(aliResult.getResult().getRespCode(), aliResult.getResult().getRespDesc());
        }
        return aliResult.getResult().getAppAuthUrl();
    }
}
